package com.doudou.oauth.validate.sms;

import com.doudou.commons.regex.RegexUtils;
import com.doudou.oauth.exception.ValidateCodeException;
import com.doudou.oauth.validate.AbstractValidateCodeProcessor;
import com.doudou.oauth.validate.ValidateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @Author: 傻男人
 * @Date: 2020/5/21 17:34
 * @Version: 1.0
 * @Description: 发送短信
 */
@Slf4j
@Component("smsValidateCodeProcessor")
public class SmsCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode> {

    /**
     * 短信验证码发送器
     */
    @Autowired
    private SmsCodeSender smsCodeSender;

    /**
     * 发送短信验证码
     */
    @Override
    protected void send(ServletWebRequest request, ValidateCode validateCode) throws Exception {
        String mobile = request.getParameter("mobile");
        if(!RegexUtils.checkMobile(mobile)){
            throw new ValidateCodeException("手机号码格式不正确");
        }
        smsCodeSender.send(mobile,validateCode.getValidateCode());
    }

}
